#include<stdio.h>
#include<stdlib.h>
#include "sqqueue.h"
#include<string.h>

//生成队列
sequeue * queue_create(){
	sequeue * q;
	if((q = (sequeue *)malloc(sizeof(sequeue))) == NULL){
		printf("init queue failed\n");
		return NULL;
	}
	memset(q->data, 0, sizeof(q->data));
	q->front = q->rear = 0;
	return q;
}
//入队
int enqueue(sequeue *sq, datatype x){
	if(sq == NULL){
		printf("sq is NULL\n");
		return -1;
	}
	//判断队列是否已满
	if((sq->rear + 1) % N == sq->front){
		printf("sq is full\n");
		return -1;
	}
	//入队
	sq->data[sq->rear] = x;
	sq->rear = (sq->rear + 1) % N;
	return 0;
}
//出队
datatype dequeue(sequeue *sq){
	if(sq == NULL){
		printf("sq is NULL\n");
		return -1;
	}
	//判断队列是否已空
	if(sq->front == sq->rear){
		printf("sq is empty\n");
		return -1;
	}
	//出队
	datatype value = sq->data[sq->front];
	sq->front = (sq->front + 1) % N;
	return value;
	
}
//队列是否为空
int queue_empty(sequeue *sq){	
	if(sq == NULL){
		printf("sq is NULL\n");
		return -1;
	}
	//判断队列是否已空
	return sq->front == sq->rear ? 1 : 0;
}
//队列是否已满
int queue_full(sequeue *sq){
	if(sq == NULL){
		printf("sq is NULL\n");
		return -1;
	}
	//判断队列是否已满
	return ((sq->rear + 1) % N == sq->front ? 1 : 0);

}
//清空队列
int queue_clear(sequeue *sq){
	if(sq == NULL){
		printf("sq is NULL\n");
		return -1;
	}
	sq->front = sq->rear = 0;
	return 0;
}
//释放队列空间
sequeue * queue_free(sequeue *sq){
	if(sq == NULL){
		printf("sq is NULL\n");
		return NULL;
	}
	free(sq);
	sq = NULL;
	return sq;
}
